package com.ygy.jxc.controller;


import com.ygy.jxc.model.BjmdNote;
import com.ygy.jxc.service.BjNoteService;
import com.ygy.jxc.util.commonPage;
import com.ygy.jxc.util.commonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@Api(tags = "BjNoteController" ,description = "bj-笔记")
@Tag(name = "BjNoteController", description = "系统管理-笔记")
@RequestMapping("/bjNote")
public class BjNoteController {

    @Autowired
   private BjNoteService noteService;

    @ApiOperation("添加笔记")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public commonResult create(@RequestBody BjmdNote note) {
        int count = noteService.create(note);
        if (count > 0) {
            return commonResult.success(count);
        }
        return commonResult.failed();
    }

    /**
     *   问题获取不到前端传递的参数  @RequestParam(value ="title") post与get的问题
     * @param title
     * @return
     */
    @ApiOperation("创建笔记，创建笔记名")
    @RequestMapping(value = "/createByTitle",method = RequestMethod.POST)
    @ResponseBody
    public commonResult createByTitle(  String title){
        System.out.println("笔记:"+title);
        int result =noteService.createByTitle(title);
        if (result > 0) {
            return commonResult.success(result);
        }
        //
        return commonResult.failed("系统暂时只能创建8条 ");
    }

    @ApiOperation("更新笔记")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public commonResult update(@RequestBody  BjmdNote note) {
        int count = noteService.update(  note);
        if (count > 0) {
            return commonResult.success("成功");
        }
        return commonResult.failed();
    }


    @ApiOperation("获取当前用户的所有笔记")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public commonResult<List<BjmdNote>> listAll() {
        System.out.println("********************");
        UsernamePasswordAuthenticationToken token = (UsernamePasswordAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
        String userName =  (String)token.getPrincipal();
        System.out.println("笔记-----------------------"+userName);
        List<BjmdNote> noteList = noteService.list(userName);

        BjmdNote bjmdNote = noteList.get(0);
        System.out.println(bjmdNote.toString());
    return commonResult.success(noteList);
    }

    @ApiOperation("分页查询笔记")
    @RequestMapping(value = "/listByName", method = RequestMethod.GET)
    @ResponseBody
    public commonResult<commonPage<BjmdNote>> listByName(@RequestParam(value = "keyword", required = false) String keyword,
                                                         @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                         @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<BjmdNote> noteList = noteService.listByName(keyword, pageSize, pageNum);
        return commonResult.success(commonPage.restPage(noteList));
    }

    @ApiOperation("删除笔记")
    @RequestMapping(value = "/deleteById",method = RequestMethod.POST)
    @ResponseBody
    public  commonResult  delectNote(Long id){
        int count = noteService.delete(id);
        if (count > 0) {
            return commonResult.success("成功");
        }
        return commonResult.failed();
    }
}
